package net.bsayiner.foreignExchange.Activities;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

import net.bsayiner.foreignExchange.R;
import net.bsayiner.foreignExchange.Utilities.Metal;
import net.bsayiner.foreignExchange.Utilities.MetalReader;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class MetalActivity extends Activity implements Runnable {

	private MetalReader metalReader;
	private ProgressDialog currencyUpdateDialog;
	private ArrayList<Metal> metals;
	private NumberFormat currencyFormat;

	private TextView txt22Buy;
	private TextView txt22Sell;
	private TextView txt18Buy;
	private TextView txt18Sell;
	private TextView txt14Buy;
	private TextView txt14Sell;
	private TextView txtQuarterBuy;
	private TextView txtQuarterSell;
	private TextView txtHalfBuy;
	private TextView txtHalfSell;
	private TextView txtGoldBuy;
	private TextView txtGoldSell;
	private TextView txtGremseBuy;
	private TextView txtGremseSell;
	private TextView txtRepublicBuy;
	private TextView txtRepublicSell;
	private TextView txtResatBuy;
	private TextView txtResatSell;
	private TextView txtHamitBuy;
	private TextView txtHamitSell;
	private TextView txtGramBuy;
	private TextView txtGramSell;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.metal_layout);
		initializeVariables();
		updateCurrencyData();
	}

	private void refreshCurrencyData() {
		currencyUpdateDialog = ProgressDialog.show(this, "", "Kur bilgisi güncelleniyor...");
		Thread thread = new Thread(this);
		thread.start();
	}

	private void initializeVariables() {
		metalReader = new MetalReader();
		metals = getIntent().getParcelableArrayListExtra("Metal");
		currencyFormat = NumberFormat.getNumberInstance(Locale.US);
		currencyFormat.setGroupingUsed(false);
		currencyFormat.setMinimumFractionDigits(2);
		currencyFormat.setMaximumFractionDigits(2);
		txt22Buy = (TextView) findViewById(R.id.txt_22_buy);
		txt22Sell = (TextView) findViewById(R.id.txt_22_sell);
		txt18Buy = (TextView) findViewById(R.id.txt_18_buy);
		txt18Sell = (TextView) findViewById(R.id.txt_18_sell);
		txt14Buy = (TextView) findViewById(R.id.txt_14_buy);
		txt14Sell = (TextView) findViewById(R.id.txt_14_sell);
		txtQuarterBuy = (TextView) findViewById(R.id.txt_quarter_buy);
		txtQuarterSell = (TextView) findViewById(R.id.txt_quarter_sell);
		txtHalfBuy = (TextView) findViewById(R.id.txt_half_buy);
		txtHalfSell = (TextView) findViewById(R.id.txt_half_sell);
		txtGoldBuy = (TextView) findViewById(R.id.txt_gold_buy);
		txtGoldSell = (TextView) findViewById(R.id.txt_gold_sell);
		txtGremseBuy = (TextView) findViewById(R.id.txt_gremse_buy);
		txtGremseSell = (TextView) findViewById(R.id.txt_gremse_sell);
		txtRepublicBuy = (TextView) findViewById(R.id.txt_republic_buy);
		txtRepublicSell = (TextView) findViewById(R.id.txt_republic_sell);
		txtResatBuy = (TextView) findViewById(R.id.txt_resat_buy);
		txtResatSell = (TextView) findViewById(R.id.txt_resat_sell);
		txtHamitBuy = (TextView) findViewById(R.id.txt_hamit_buy);
		txtHamitSell = (TextView) findViewById(R.id.txt_hamit_sell);
		txtGramBuy = (TextView) findViewById(R.id.txt_gram_buy);
		txtGramSell = (TextView) findViewById(R.id.txt_gram_sell);
	}

	public void run() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
				metals = metalReader.getCurrenciesFromWeb();
		handler.sendEmptyMessage(0);
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			updateCurrencyData();
			currencyUpdateDialog.dismiss();
		}
	};

	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.menu_structure, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_update:
			refreshCurrencyData();
			return true;
		case R.id.menu_about:
			PackageInfo packageInfo = null;
			try {
				packageInfo = getPackageManager().getPackageInfo("net.bsayiner.foreignExchange", 0);
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
			final AlertDialog.Builder alert = new AlertDialog.Builder(this);
			alert.setTitle("Türk Lirası v" + packageInfo.versionName);
			alert.setMessage("Bu program Bora SAYINER tarafından yazılmış ve  OSI onaylı açık kaynak lisansı altında dağıtılmaktadır. Soru ve görüşleriniz için bsayiner@bsayiner.net adresine e-posta gönderebilirsiniz.");
			alert.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			alert.show();
			return true;
		case R.id.menu_exit:
			System.exit(0);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void updateCurrencyData() {
		txt22Buy.setText(currencyFormat.format((metals.get(0).getBuying())));
		txt22Sell.setText(currencyFormat.format(metals.get(0).getSelling()));
		txtGramBuy.setText(currencyFormat.format(metals.get(1).getBuying()));
		txtGramSell.setText(currencyFormat.format(metals.get(1).getSelling()));
		txt18Buy.setText(currencyFormat.format(metals.get(2).getBuying()));
		txt18Sell.setText(currencyFormat.format(metals.get(2).getSelling()));
		txt14Buy.setText(currencyFormat.format(metals.get(3).getBuying()));
		txt14Sell.setText(currencyFormat.format(metals.get(3).getSelling()));
		txtQuarterBuy.setText(currencyFormat.format(metals.get(4).getBuying()));
		txtQuarterSell.setText(currencyFormat.format(metals.get(4).getSelling()));
		txtHalfBuy.setText(currencyFormat.format(metals.get(5).getBuying()));
		txtHalfSell.setText(currencyFormat.format(metals.get(5).getSelling()));
		txtGoldBuy.setText(currencyFormat.format(metals.get(6).getBuying()));
		txtGoldSell.setText(currencyFormat.format(metals.get(6).getSelling()));
		txtGremseBuy.setText(currencyFormat.format(metals.get(7).getBuying()));
		txtGremseSell.setText(currencyFormat.format(metals.get(7).getSelling()));
		txtRepublicBuy.setText(currencyFormat.format(metals.get(8).getBuying()));
		txtRepublicSell.setText(currencyFormat.format(metals.get(8).getSelling()));
		txtResatBuy.setText(currencyFormat.format(metals.get(9).getBuying()));
		txtResatSell.setText(currencyFormat.format(metals.get(9).getSelling()));
		txtHamitBuy.setText(currencyFormat.format(metals.get(10).getBuying()));
		txtHamitSell.setText(currencyFormat.format(metals.get(10).getSelling()));
		
	}
}
